from database.db_handler import MysqlHander
import socket

class DbBusiness:
    def __init__(self):
        self.db = MysqlHander("config.ini")
        host_name = socket.gethostname()
        self.host = socket.gethostbyname(host_name)
        pass

    def query_stock_code(self):
        sql = "select Fcode,Fname,Fexchange_id from entity_gupiao;"
        res = []
        data = self.db.query_sql(sql)
        for d in data:
            res.append(d[0])
        return res

    def query_diqu(self):
        sql = "select Fentity_name from market_entity_data where Fentity_type = 200;"
        res = []
        data = self.db.query_sql(sql)
        for d in data:
            res.append(d[0])
        return res

    def query_market_entity(self, data_type):
        sql = "select Fentity_name from market_entity_data where Fentity_type = " + str(data_type) + ";"
        res = []
        data = self.db.query_sql(sql)
        for d in data:
            res.append(d[0])
        return res

    def query_kejipinpai(self):
        sql = "select Fentity_name from market_entity_data where Fentity_type = 101;"
        res = []
        data = self.db.query_sql(sql)
        for d in data:
            res.append(d[0])
        return res

    def query_stock_code_name(self):
        sql = "select Fcode,Fname,Fexchange_id from entity_gupiao;"
        res = {}
        data = self.db.query_sql(sql)
        for d in data:
            res[d[0]] = d[1]
        return res

    def query_gudong_code_name(self):
        sql = "select Fcode,Fname from entity_gudong;"
        res = {}
        data = self.db.query_sql(sql)
        for d in data:
            res[d[0]] = d[1]
        return res

    def query_stock_code_exchange(self):
        sql = "select Fcode,Fname,Fexchange_id from entity_gupiao;"
        res = {}
        data = self.db.query_sql(sql)
        for d in data:
            res[d[0]] = d[2]
        return res
 
    def query_dazongshangpin(self):
        sql = "select Fentity_name from market_entity_data where Fentity_type=300;"
        res = []
        data = self.db.query_sql(sql)
        for d in data:
            res.append(d[0])
        return res

    def query_gdp80(self):
        sql = "select Fentity_name from market_entity_data where Fentity_type=500;"
        res = []
        data = self.db.query_sql(sql)
        for d in data:
            res.append(d[0])
        return res

    '''
    def query_proxy(self):
        print("host:" + self.host)
        sql = "select Fip,Fport from proxy_ip where Fexpire_time > ADDDATE(now(),interval 20 second) and Fhost='" + self.host + "' ORDER BY RAND() limit 1;"
        data = self.db.query_sql(sql)
        for d in data:
            proxy = "http://guangtoufangyang:wAy8G363bfWmEZH@" + d[0] + ":" + str(d[1])
            return proxy
        return ""
    '''
        
    def query_proxy(self):
        print("host:" + self.host)
        sql = "select Fip,Fport from proxy_ip where Fexpire_time > ADDDATE(now(),interval 5 second) ORDER BY RAND() limit 1;"
        data = self.db.query_sql(sql)
        for d in data:
            #proxy = "http://guangtoufangyang:wAy8G363bfWmEZH@" + d[0] + ":" + str(d[1])
            proxy = "http://" + d[0] + ":" + str(d[1])
            return proxy
        return ""

